package com.example.tellwin;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.apkfuns.logutils.LogUtils;
import com.example.tellwin.base.CpBaseActivty;
import com.example.tellwin.common.Common;
import com.example.tellwin.event.ActDetailEvent;
import com.example.tellwin.event.ShareIntentEvent;
import com.example.tellwin.home.StudentHomeFragment;
import com.example.tellwin.home.TeacherHomeFragment;
import com.example.tellwin.home.act.ActDetailActivity;
import com.example.tellwin.home.act.QuestionDetailActivity;
import com.example.tellwin.mine.MineFragment;
import com.example.tellwin.question.QuestionFragment;
import com.example.tellwin.utils.CommonUtils;
import com.example.tellwin.utils.ImageUtils;
import com.example.tellwin.utils.LogUtil;
import com.example.tellwin.utils.StatusBarUtil;
import com.example.tellwin.utils.ToastUtil;
import com.example.tellwin.view.FragmentTabMenuView;
import com.example.tellwin.view.OnTabMenuClickListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.tellwin.common.Common.PhotoAlbumRequestCode;
import static com.example.tellwin.common.Common.TakePhotoRequestCode;

public class MainActivity extends CpBaseActivty implements OnTabMenuClickListener {

    @BindView(R.id.tabmenu)
    FragmentTabMenuView tabMenu;

    private final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        //MainApplication.Identity = getIntent().getStringExtra(Common.IDENTITY);
        initViews();
        initEvents();
        initDatas();
        LogUtils.e(CommonUtils.getSingInfo(this , getPackageName() ,CommonUtils.MD5));
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        shareIntent();
    }

    @Override
    protected void initViews() {
        LogUtil.e(TAG, MainApplication.Identity);
        if (MainApplication.Identity.equals(Common.NORMAL)) {
            tabMenu.addMenu(getResources().getString(R.string.home), R.mipmap.icon_product_inactive, R.mipmap.icon_product_active, StudentHomeFragment.class);
            tabMenu.addMenu(getResources().getString(R.string.ask_question), R.mipmap.icon_social_inactive, R.mipmap.icon_question_active, QuestionFragment.class);
            tabMenu.addMenu(getResources().getString(R.string.mine), R.mipmap.icon_mine_inactive, R.mipmap.icon_mine_active, MineFragment.class);
        } else {
            tabMenu.addMenu(getResources().getString(R.string.home), R.mipmap.icon_product_inactive, R.mipmap.icon_product_active, StudentHomeFragment.class);
            tabMenu.addMenu(getResources().getString(R.string.answer_), R.mipmap.icon_social_inactive, R.mipmap.icon_question_active, TeacherHomeFragment.class);
            tabMenu.addMenu(getResources().getString(R.string.mine), R.mipmap.icon_mine_inactive, R.mipmap.icon_mine_active, MineFragment.class);
        }

        tabMenu.refreshMenuViewAt(getSupportFragmentManager(), 0);
    }

    @Override
    protected void initEvents() {
        tabMenu.setOnTabMenuClickListener(this);
        CommonUtils.requestPermissions(this);
    }

    @Override
    protected void initDatas() {
        CallService.start(this);
        shareIntent();
    }

    private void shareIntent() {
        String shareType = AppHelper.getInstance().getShareType();
        String shareId = AppHelper.getInstance().getShareId();
        LogUtil.d(shareType);
        LogUtil.d(shareId);
        if ("1".equals(shareType)) {//活动
            Intent intent = new Intent(this , ActDetailActivity.class);
            intent.putExtra(Common.ACTIVITY_ID , shareId);
            startActivity(intent);
        } else if ("2".equals(shareType)) {//问题
            Intent intent = new Intent(this , QuestionDetailActivity.class);
            intent.putExtra(Common.QUESTION , shareId);
            startActivity(intent);
        }
        AppHelper.getInstance().setShareType("");
        AppHelper.getInstance().setShareId("");
    }

    @Override
    public void onTabMenuClick(View container, int lastChecked, int nowChecked) {
        switch (nowChecked) {
            case 0:
                StatusBarUtil.setTranslucentStatus(this, false);
                break;
            case 1:
                StatusBarUtil.setTranslucentStatus(this, false);
                if (MainApplication.Identity.equals(Common.NORMAL)) {

                } else {
                    Fragment fragment = tabMenu.getFragment(nowChecked);
                    if(fragment!=null && fragment instanceof TeacherHomeFragment) {
                        ((TeacherHomeFragment)fragment).refresh();
                    }
                }
                break;
            case 2:
                StatusBarUtil.setTranslucentStatus(this, true);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.e(requestCode + "-" +resultCode);
        if (resultCode == Activity.RESULT_OK) {
            QuestionFragment questionFragment = (QuestionFragment) tabMenu.getFragment(1);
            switch (requestCode) {
                case TakePhotoRequestCode:
                    questionFragment.onPhotoResult();
                    break;
                case PhotoAlbumRequestCode:
                    if (data != null) {
                        Uri uri = data.getData();
                        questionFragment.addPhotoPath(ImageUtils.uriToRealPath(this, uri));
                    }
                    break;
            }
        }
    }
    @Subscribe
    public void onEvent(ShareIntentEvent event) {
        shareIntent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        CallService.stop(this);
    }
}
